package com.example.free;

import java.io.IOException;
import java.io.InputStream;















import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.renderscript.RenderScript;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.StackView;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
	private ImageView imageview;
	private SeekBar   seekbar;
    private StackBlurManager _stackBlurManager;
    private ImageButton imageButton1;
    private static int RESULT_LOAD_IMAGE = 1;
    private ImageView imageView;

	

	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		imageview=(ImageView) findViewById(R.id.imageview);
		seekbar=(SeekBar) findViewById(R.id.blur_amount);
		/*imageButton1=(ImageButton) findViewById(R.id.imageButton1);
		imageButton1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 Intent i = new Intent(
	                        Intent.ACTION_PICK,
	                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
	  
	                startActivityForResult(i, RESULT_LOAD_IMAGE);
				
			}
			
		});*/
		
		final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
//        Resources res = getResources();
    	_stackBlurManager=new  StackBlurManager(getBitmapFromAsset(this, "image.jpg"));

		seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {


				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				_stackBlurManager.process(progress*4);
//				imageview.setImageBitmap(_stackBlurManager.returnBlurredImage());
				try
				{
					wallpaperManager.setBitmap(_stackBlurManager.returnBlurredImage());
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
				
			}
		});

		}

	





	private Bitmap getBitmapFromAsset(Context context, String strName) {
		AssetManager assetManager=context.getAssets();
		InputStream istr;
		Bitmap bitmap =null;
		try {
			 istr = assetManager.open(strName);
	            bitmap = BitmapFactory.decodeStream(istr);
		} catch (Exception e) {
			return null;
		}
		return bitmap;
	}
/*	  
	@Override
	    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	        super.onActivityResult(requestCode, resultCode, data);
	  
	        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
	            Uri selectedImage = data.getData();
	            String[] filePathColumn = { MediaStore.Images.Media.DATA };
	  
	            Cursor cursor = getContentResolver().query(selectedImage,
	                    filePathColumn, null, null, null);
	            cursor.moveToFirst();
	  
	            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
	            String picturePath = cursor.getString(columnIndex);
	            cursor.close();
	  
	            ImageView imageView = (ImageView) findViewById(R.id.imageView);
	            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
	  
	        }
	}*/
		
	
		
	

}
